package com.zjc.study.collection;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-07-14  08:37
 * 集合的遍历
 */
public class Collection02 {
    public static void main(String[] args) {
        // 集合的遍历
        // 1. 迭代器
        // 2. 增强for循环
        // 3. for循环
        ArrayList<String> list = new ArrayList<>();
        list.add("唐僧");
        list.add("孙悟空");
        list.add("猪八戒");
        list.add("沙僧");

        System.out.println("1. 迭代器");
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            String element = iterator.next();
            System.out.println(element);
        }
        System.out.println("-------------------");
        System.out.println("2. 增强for循环");
        for(String element : list){
            System.out.println(element);
        }
//        list.forEach(System.out::println);
        System.out.println("-------------------");
        System.out.println("3. for循环");
        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }
    }
}
